﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InfoConfig;
using MINERP.MODEL.Statistics;

namespace MINERP.InfoConfig
{
    public partial class FrmSalesmanManager : Form
    {
        private static FrmSalesmanManager _instance = null;
        SalesmanBLL _bll = new SalesmanBLL();

        private FrmSalesmanManager()
        {
            InitializeComponent();
        }

        public static FrmSalesmanManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmSalesmanManager();
                }
                return _instance;
            }
        }
        private void FrmSalesmanManager_Load(object sender, EventArgs e)
        {
            LinkgcSalesman();
        }


        private void LinkgcSalesman()
        {
            gcSalesman.DataSource = _bll.GetSalesmanInfoList();
            gcSalesman.RefreshDataSource();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (CheckData())
            {
                Salesman_Info salesmanInfo = new Salesman_Info()
                {
                    Balance = 0,
                    Remark = txtRemark.Text.Trim(),
                    SalesmanName = txtSalesmanName.Text.Trim(),
                    Tel1 = txtTel.Text.Trim(),
                    Address = txtAddress.Text.Trim(),
                    Describe = txtOtherName.Text.Trim(),
                    Fax = txtFax.Text.Trim(),
                    RequestPaperName = txtRequestPaperName.Text.Trim()
                }; 
                int ret = _bll.Insert(salesmanInfo);
                if (ret == 0)
                {
                    LinkgcSalesman();
                    MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 1)
                {
                    MessageBox.Show("数据库中已存在该业务员的名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("数据库连接错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                LinkgcSalesman();
            }
        }

        private bool CheckData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(txtSalesmanName.Text.Trim()))
            {
                ret = false;
                txtSalesmanName.Focus();
                MessageBox.Show("请输入业务员名称!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            return ret;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (CheckData())
            {
                Salesman_Info salesmanInfo = (Salesman_Info)gvSalesman.GetFocusedRow();
                if (salesmanInfo != null)
                {
                    salesmanInfo.Remark = txtRemark.Text.Trim();
                    salesmanInfo.SalesmanName = txtSalesmanName.Text.Trim();
                    salesmanInfo.Tel1 = txtTel.Text.Trim();
                    salesmanInfo.Address = txtAddress.Text.Trim();
                    salesmanInfo.Describe = txtOtherName.Text.Trim();
                    salesmanInfo.Fax = txtFax.Text.Trim();
                    salesmanInfo.RequestPaperName = txtRequestPaperName.Text.Trim();

                    int ret = _bll.Update(salesmanInfo);
                    if (ret == 0)
                    {
                        
                        MessageBox.Show("更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (ret == 1)
                    {
                        MessageBox.Show("数据库中已存在该业务员的名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("数据库连接错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("请选择要更新的业务员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                LinkgcSalesman();
            }
        }

        private void FrmSalesmanManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            _instance = null;
        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }
    }
}
